package com.musicmall.controller.portal;

import com.musicmall.common.Const;
import com.musicmall.common.ResponseCode;
import com.musicmall.common.ServerResponse;
import com.musicmall.dao.UserMapper;
import com.musicmall.pojo.User;
import com.musicmall.service.IUserService;
import org.apache.catalina.Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user/")
public class UserController {
    @Autowired
    private IUserService iUserService;

    @GetMapping("test.do")
    public String test(){
        return "testtest!!!";
    }

    @PostMapping("login.do")
    public ServerResponse login(String username, String password, HttpSession session){
        ServerResponse response = iUserService.login(username, password);
        if(response.isSuccess()){
            User user = (User)response.getData();
            session.setAttribute(Const.CURRENT_USER,user);
        }
        return response;

    }

    @GetMapping("logout.do")
    public ServerResponse logout(HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        return ServerResponse.createBySuccess();

    }

    @PostMapping("register.do")
    public ServerResponse register(User user){
        return iUserService.register(user);

    }

    @GetMapping("check_valid.do")
    public ServerResponse checkValid(String str,String type){
        return iUserService.checkvalid(str, type);

    }

    @GetMapping("get_information.do")
    public ServerResponse getInformation(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
                    ResponseCode.NEED_LOGIN.getDesc());
        }
        return iUserService.getInformation(user.getId());

    }





}
